package com.chen.example;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;

/**
 * ClassName: MyActionListener
 * Package: com.chen.example
 * Description:  计算机实现逻辑
 *
 * @Author 陈俊辉
 * @Create 2024/4/20 6:12
 * @Version 1.0
 */
public class MyActionListener1 implements ActionListener {
    //操作的文本框
    JTextField jTextField;

    public MyActionListener1(JTextField jTextField) {
        this.jTextField = jTextField;
    }

    //用于判断是否重新开始
    private boolean start = true;
    //存放计算结果
    private double result = 0;
    //存放指令命令
    private String command = "=";
    @Override
    public void actionPerformed(ActionEvent e) {
        //获得命令
        String input = e.getActionCommand();
        //实现逻辑
        if(start) {
            //设置字符串为1-9
            if((int)input.charAt(0)>=48 && (int)input.charAt(0)<=57
                    && input.length() == 1 ) {
                jTextField.setText(""+input);
            }
            start = false;
            if(input.equals("C")) {
                jTextField.setText("");
                result=0;
            }


        }
        //0~9数字等非运算符
        else if((int)input.charAt(0)>=48 && (int)input.charAt(0)<=57
                && input.length() == 1 || input.equals(".")){
            jTextField.setText(jTextField.getText()+input);
        }
        //实现所有 数组+运算符 的运算

        //实现清零键
        else if(input.equals("C")){
            jTextField.setText("");
        }
        //实现退格键,就是减去上一个字符
        else if(input.equals("Back")) {
            if(jTextField.getText().length() > 0){
                jTextField.setText(jTextField.getText().substring(0,jTextField.getText().length()-1));
            }
        }
        //实现正弦三角函数
        else if(input.equals("sin")) {
            result = Math.sin(Double.parseDouble(jTextField.getText()));
            jTextField.setText(""+getPrettyNumber(Double.toString(result)));
            start = true;
        }
        //实现余弦三角函数
        else if(input.equals("cos")) {
            result = Math.cos(Double.parseDouble(jTextField.getText()));
            jTextField.setText(""+getPrettyNumber(Double.toString(result)));
            start = true;
        }
        //实现余切三角函数
        else if(input.equals("cot")) {
            result = 1.0/Math.tan(Double.parseDouble(jTextField.getText()));
            jTextField.setText(""+getPrettyNumber(Double.toString(result)));
            start = true;
        }
        //实现正切三角函数
        else if(input.equals("tan")) {
            result = Math.tan(Double.parseDouble(jTextField.getText()));
            jTextField.setText(""+getPrettyNumber(Double.toString(result)));
            start = true;
        }
        //实现十进制到二进制的转化
        else if(input.equals("to 2")) {
            String result2 = Integer.toBinaryString(Integer.parseInt(jTextField.getText()));
            jTextField.setText(""+getPrettyNumber(result2));
            start = true;
        }
        //实现二进制到十进制的转化
        else if(input.equals("2 to 10")) {
            try {
                String result2 = Integer.valueOf(jTextField.getText(),2).toString();
                jTextField.setText(""+getPrettyNumber(result2));
            }catch(NumberFormatException exception) {
                JOptionPane.showMessageDialog(null, "对不起，数字错误，请重新输入！", "Error!", JOptionPane.ERROR_MESSAGE);
                throw new NumberFormatException("数字格式错误");
            }finally {
                start = true;
            }
        }
        //实现十进制到八进制的转化
        else if(input.equals("to 8")) {
            String result2 = String.format("%o", jTextField.getText());
            jTextField.setText(""+getPrettyNumber(result2));
            start = true;
        }
        //实现八进制到十进制的转化
        else if(input.equals("8 to 10")) {
            try {
                String result2 = Integer.valueOf(jTextField.getText(),8).toString();
                jTextField.setText(""+getPrettyNumber(result2));
            }catch(NumberFormatException exception) {
                JOptionPane.showMessageDialog(null, "对不起，数字错误，请重新输入！", "Error!", JOptionPane.ERROR_MESSAGE);
                throw new NumberFormatException("数字格式错误");
            }finally {
                start = true;
            }
        }
        //实现1/x
        else if(input.equals("1/X")) {
            result = 1 / Double.parseDouble(jTextField.getText());
            jTextField.setText(""+getPrettyNumber(Double.toString(result)));
            start = true;
        }
        //实现平方计算
        else if(input.equals("X^2")) {
            result = Math.pow(Double.parseDouble(jTextField.getText()), 2);
            jTextField.setText(""+getPrettyNumber(Double.toString(result)));
            start = true;
        }
        //实现立方计算
        else if(input.equals("X^3")) {
            result = Math.pow(Double.parseDouble(jTextField.getText()), 3);
            jTextField.setText(""+getPrettyNumber(Double.toString(result)));
            start = true;
        }
        //实现阶乘
        else if(input.equals("X!")) {
            if(Double.parseDouble(jTextField.getText()) < 0) {
                JOptionPane.showMessageDialog(null, "对不起，阶乘计算不能为负数", "Error!", JOptionPane.ERROR_MESSAGE);
                jTextField.setText("对不起，阶乘计算不能为负数");
                start = true;
                throw new IllegalArgumentException("阶乘计算出现负数");
            }else {
                int sum;
                sum = factorial(Integer.parseInt(jTextField.getText()));
                jTextField.setText(Integer.toString(sum));
                start = true;
            }

        }
        //实现百分号计算
        else if(input.equals("%")) {
            result = Double.parseDouble(jTextField.getText())/ 100.0;
            jTextField.setText(""+getPrettyNumber(Double.toString(result)));
            start = true;
        }
        //实现开平方根
        else if(input.equals("2|X")) {
            result = Math.sqrt(Double.parseDouble(jTextField.getText()));
            jTextField.setText(""+getPrettyNumber(Double.toString(result)));
            start = true;
        }
        //实现开立方根
        else if(input.equals("3|X")) {
            result = Math.pow(Double.parseDouble(jTextField.getText()),1.0/3);
            jTextField.setText(""+getPrettyNumber(Double.toString(result)));
            start = true;
        }
        //实现获取当前时间
        else if(input.equals("TIME")) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            jTextField.setText(df.format(System.currentTimeMillis()));
            start = true;
        }

        //实现加减乘除等 数字+运算符+数字 形式的运算
        else {
            if(!start) {

                if(command.equals("+"))
                    result += Double.parseDouble(jTextField.getText());
                else if(command.equals("-"))
                    result -= Double.parseDouble(jTextField.getText());
                else if(command.equals("*"))
                    result *= Double.parseDouble(jTextField.getText());
                else if(command.equals("/")) {
                    if(Double.parseDouble(jTextField.getText()) != 0) {
                        result /= Double.parseDouble(jTextField.getText());
                    }else {
                        jTextField.setText(""+"对不起，除数不能为零");
                        JOptionPane.showMessageDialog(null, "对不起，除数不能为零", "Error!", JOptionPane.ERROR_MESSAGE);
                        command = "=";
                        start = true;
                        throw new ArithmeticException("除数为零");
                    }

                }
                else if(command.equals("="))
                    result = Double.parseDouble(jTextField.getText());

                else if(command.equals("X^Y"))
                    result = Math.pow(result, Double.parseDouble(jTextField.getText()));
                jTextField.setText(""+getPrettyNumber(Double.toString(result)));
                command = input;
                start = true;
            }
        }
    }

    //去掉小数点后没用的0
    public static String getPrettyNumber(String number) {
        return BigDecimal.valueOf(Double.parseDouble(number))
                .stripTrailingZeros().toPlainString();
    }
    //用循环计算阶乘
    public static int factorial(int num) {
        int sum = 1;
        for(int i = 1;i <= num; i++){
            sum *= i;
        }
    return sum;
    }
}
